package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :ttm
 * @description :
 * @create :2021-12-25 13:39:00
 */
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    //springmvc对象传值，如果传递的参数与实体类的属性名一致，则可以使用实体类获取，自动映射
    //@RequestBody 不加：因为这个注解只获取json数据
    public String list(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);
        String urlparam = this.makeUrlParam(searchParam);
        //品牌面包屑
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());

        Map<String, Object> orderMap = this.makeOrderMap(searchParam.getOrder());
        //平台属性面包屑
        List<Map<String, Object>> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("urlParam", urlparam);
        model.addAllAttributes(result.getData());
        model.addAttribute("searchParam", searchParam);
        model.addAttribute("trademarkParam", trademarkParam);
        model.addAttribute("propsParamList", propsParamList);
        model.addAttribute("orderMap", orderMap);

        return "list/index";
    }

    /**
     * 设置排序
     *
     * @param order
     * @return
     */
    private Map<String, Object> makeOrderMap(String order) {
        HashMap<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
        } else {
            map.put("type", 1);
            map.put("sort", "desc");
        }
        return map;
    }

    //平台属性面包屑
    private List<Map<String, Object>> makePropsParamList(String[] props) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("attrId", split[0]);
                    map.put("attrValue", split[1]);
                    map.put("attrName", split[2]);
                    //将集合添加到map
                    list.add(map);
                }
            }
        }
        return list;
    }

    //品牌面包屑
    private String makeTrademarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return null;
    }
    //平台属性面包屑


    /**
     * 拼接用户查询的url
     *
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        //根据关键词检索
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            sb.append("Keyword=").append(searchParam.getKeyword());
        }
        //根据三级分类id检索
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            sb.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //根据二级分类id检索
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            sb.append("category2Id=").append(searchParam.getCategory2Id());
        }
        //根据一级分类id检索
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            sb.append("category1Id=").append(searchParam.getCategory1Id());
        }
        //根据品牌检索
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            if (sb.length() > 0) {
                sb.append("&trademark=").append(searchParam.getTrademark());
            }
        }

        //根据平台属性检索
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                if (sb.length() > 0) {
                    sb.append("&props=").append(prop);
                }
            }
        }
        //返回拼接后的url
        return "list.html?" + sb.toString();

    }

}
